package com.aat.dxfy.http;

import java.io.File;
import java.io.FileInputStream;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

/**
 * Example how to use unbuffered chunk-encoded POST request.
 */
public class ClientChunkEncodedPost {

	public static void main(String[] args) throws Exception {
		if (args.length != 1) {
			System.out.println("File path not given");
			System.exit(1);
		}
		CloseableHttpClient httpclient = HttpClients.createDefault();
		try {
			HttpPost httppost = new HttpPost("http://httpbin.org/post");

			File file = new File(args[0]);

			InputStreamEntity reqEntity = new InputStreamEntity(new FileInputStream(file), -1,
					ContentType.APPLICATION_OCTET_STREAM);
			reqEntity.setChunked(true);

			httppost.setEntity(reqEntity);

			System.out.println("Executing request: " + httppost.getRequestLine());
			
			
			
			CloseableHttpResponse response = httpclient.execute(httppost);
			try {
				System.out.println("----------------------------------------");
				System.out.println(response.getStatusLine());
				System.out.println(EntityUtils.toString(response.getEntity()));
			} finally {
				response.close();
			}
		} finally {
			httpclient.close();
		}
	}

}